package com.itsol.user.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.itsol.user.dao.UserDAO;
import com.itsol.user.entity.User;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
    private UserDAO userDAO;
 
    @RequestMapping({"/index", "/", ""})
    public String listContacts(Map<String, Object> map) {
 
        map.put("user", new User());
        map.put("userList", userDAO.listUser());
 
        return "user/user";
    }
 
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("user")
    User user, BindingResult result) {
 
        userDAO.addUser(user);
        return "redirect:/user/index";
    }
 
    @RequestMapping("/delete/{userId}")
    public String deleteContact(@PathVariable("userId")
    		Integer userId) {
 
    	userDAO.removeUser(userId);
        return "redirect:/user/index";
    }
}
